
import request from '@/base/request'
import router from '@/router'
import { ElMessage } from 'element-plus'
import {ref,nextTick} from 'vue'
import ExamDetail from '@/types/examDetail'
import NeedProblemPeople from '@/types/needProblemPeople'

export default function(){
    const examDetail=ref<ExamDetail>({
        id:0,
        examRegisterUserList:[],
    })

    const examId=ref()

    const teacherComment=ref({
        id:0,
        comment:""
    })
    const commentInputRef=ref<Array<any>>([])
    const showTeacherComment=ref<{ [key: number]: boolean }>({})

    const needProblemList=ref<Array<NeedProblemPeople>>()

    function init(index:number){
        teacherComment.value.id= examDetail.value.examRegisterUserList[index].id,
        teacherComment.value.comment= examDetail.value.examRegisterUserList[index].teacherComment,
        showTeacherComment.value[index]=true;
        nextTick(()=>{
            commentInputRef.value[index].focus()
        })
    }

    async function handleBlur(index){
        examDetail.value.examRegisterUserList[index].teacherComment=teacherComment.value.comment
        showTeacherComment.value[index]=false;
        const response= (await request.post(`/problem/exam/admin/addTeachComment`,teacherComment.value)).data
        if(response.code!==200)ElMessage.error(`保存失败,请重新选择后重试:${response.msg}`)
        
    }



    async function getUserExamDetail(examId:number) {
        const response= (await request.get(`/problem/exam/admin/getUserExamDetail/${examId}`)).data
        if(response.code===200){
            examDetail.value=response.data
        }else{
            ElMessage.error(`获取失败${response.msg}`)
            router.push('/admin/exam')
        }
    }

    async function updateStatus(status:number,index:number) {
        const response=(await request.put(`/problem/exam/admin/updateStatus/${examDetail.value.examRegisterUserList[index].id}/${status}`)).data
        if(response.code===200){
            examDetail.value.examRegisterUserList[index].status=status
        }else{
            ElMessage.error(`修改失败:${response.msg}`)
        }
    }

    async function delRegisterUser(id:number,examId:number) {
        const response=(await request.delete(`/problem/exam/admin/delRegisterUserById/${id}`)).data
        if(response.code===200){
            getUserExamDetail(examId)
        }else{
            ElMessage.error(`删除失败${response.msg}`)
        }
    }


    async function autoJudge(examId) {
        for(let i=0;i<examDetail.value.examRegisterUserList.length;i++){
            examDetail.value.examRegisterUserList[i].status=3
        }

        const response=(await request.get(`/problem/exam/admin/autoJudge/${examId}`)).data
        if(response.code===200){
            ElMessage.success('批阅完成')
            getUserExamDetail(examId)
        }else{
            ElMessage.error(`提交失败${response.msg}`)
        }
    }

    async function getNeedPeopleProblemList(examId:number) {
        const response=(await request.get(`/problem/exam/admin/getNeedProblemList/${examId}`)).data
        if(response.code===200){
            needProblemList.value=response.data;    
        }else{
            ElMessage.error(`获取失败:${response.msg}`)
        }
    }

    return {examDetail,teacherComment,commentInputRef,showTeacherComment,needProblemList,
        getNeedPeopleProblemList,autoJudge,delRegisterUser,updateStatus,init,handleBlur,getUserExamDetail}
}